home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / remin301.zip / REMIN300.ZIP / UTILS.C < prev    next >
C/C++ Source or Header  |  1992-11-10  |  7KB  |  220 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /*  UTILS.C                                                    */
  4. /*                                                             */
  5. /*  Useful utility functions.                                  */
  6. /*                                                             */
  7. /*  This file is part of REMIND.                               */
  8. /*  Copyright (C) 1991 by David F. Skoll.                      */
  9. /*                                                             */
  10. /***************************************************************/
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include "config.h"
  15. #ifdef HAVE_STDLIB_H
  16. #include <stdlib.h>
  17. #endif
  18. #ifdef HAVE_MALLOC_H
  19. #include <malloc.h>
  20. #endif
  21.  
  22. #define UPPER(c) ( ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c) )
  23.  
  24. /***************************************************************/
  25. /*                                                             */
  26. /*  StrEq                                                      */
  27. /*                                                             */
  28. /*  Are two strings equal?                                     */
  29. /*                                                             */
  30. /***************************************************************/
  31. #ifdef HAVE_PROTOS
  32. PUBLIC int StrEq(const char *s1, const char *s2)
  33. #else
  34. int StrEq(s1, s2)
  35. char *s1, *s2;
  36. #endif
  37. {
  38.    while (*s1 && *s2) {
  39.       if (*s1++ != *s2++) return 0;
  40.    }
  41.    if (*s1 || *s2) return 0 ; else return 1;
  42. }
  43.  
  44. /***************************************************************/
  45. /*                                                             */
  46. /*  StriEq                                                     */
  47. /*                                                             */
  48. /*  Are two strings equal, ignoring case?                      */
  49. /*                                                             */
  50. /***************************************************************/
  51. #ifdef HAVE_PROTOS
  52. PUBLIC int StriEq(const char *s1, const char *s2)
  53. #else
  54. int StriEq(s1, s2)
  55. char *s1, *s2;
  56. #endif
  57. {
  58.    while (*s1 && *s2) {
  59.       if (UPPER(*s1) != UPPER(*s2)) return 0;
  60.       s1++;
  61.       s2++;
  62.    }
  63.    if (*s1 || *s2) return 0 ; else return 1;
  64. }
  65.  
  66. /***************************************************************/
  67. /*                                                             */
  68. /*  StrinEq                                                    */
  69. /*  Are two strings equal to a given number of chars?          */
  70. /*                                                             */
  71. /***************************************************************/
  72. #ifdef HAVE_PROTOS
  73. PUBLIC int StrinEq(const char *s1, const char *s2, int n)
  74. #else
  75. int StrinEq(s1, s2, n)
  76. char *s1, *s2;
  77. int n;
  78. #endif
  79. {
  80.    while (*s1 && *s2 && n--) {
  81.       if (UPPER(*s1) != UPPER(*s2)) return 0;
  82.       s1++;
  83.       s2++;
  84.    }
  85.    if (n && (*s1 || *s2)) return 0 ; else return 1;
  86. }
  87.  
  88. /***************************************************************/
  89. /*                                                             */
  90. /*  StrnCpy                                                    */
  91. /*                                                             */
  92. /*  Just like strncpy EXCEPT we ALWAYS copy the trailing 0.    */
  93. /*                                                             */
  94. /***************************************************************/
  95. #ifdef HAVE_PROTOS
  96. PUBLIC char *StrnCpy(char *dest, const char *source, int n)
  97. #else
  98. char *StrnCpy(dest, source, n)
  99. char *dest, *source;
  100. int n;
  101. #endif
  102. {
  103.    register char *odest = dest;
  104.  
  105.    while (n-- && (*dest++ = *source++)) ;
  106.    if (*(dest-1)) *dest = 0;
  107.    return odest;
  108. }
  109.  
  110. /***************************************************************/
  111. /*                                                             */
  112. /*  StrMatch                                                   */
  113. /*                                                             */
  114. /*  Checks that two strings match (case-insensitive) to at     */
  115. /*  least the specified number of characters, or the length    */
  116. /*  of the first string, whichever is greater.                 */
  117. /*                                                             */
  118. /***************************************************************/
  119. #ifdef HAVE_PROTOS
  120. PUBLIC int StrMatch(const char *s1, const char *s2, int n)
  121. #else
  122. int StrMatch(s1, s2, n)
  123. char *s1, *s2;
  124. int n;
  125. #endif
  126. {
  127.    int l;
  128.    if ((l = strlen(s1)) < n) return 0;
  129.    return StrinEq(s1, s2, l);
  130. }
  131.  
  132. /***************************************************************/
  133. /*                                                             */
  134. /*  StrinCmp - compare strings, case-insensitive               */
  135. /*                                                             */
  136. /***************************************************************/
  137. #ifdef HAVE_PROTOS
  138. PUBLIC int StrinCmp(const char *s1, const char *s2, int n)
  139. #else
  140. int StrinCmp(s1, s2, n)
  141. char *s1, *s2;
  142. int n;
  143. #endif
  144. {
  145.    register int r;
  146.    while (n && *s1 && *s2) {
  147.       n--;
  148.       r = UPPER(*s1) - UPPER(*s2);
  149.       if (r) return r;
  150.       s1++;
  151.       s2++;
  152.    }
  153.    if (n) return (UPPER(*s1) - UPPER(*s2)); else return 0;
  154. }
  155.  
  156. /***************************************************************/
  157. /*                                                             */
  158. /*  StrDup                                                     */
  159. /*                                                             */
  160. /*  Like ANSI strdup                                           */
  161. /*                                                             */
  162. /***************************************************************/
  163. #ifdef HAVE_PROTOS
  164. PUBLIC char *StrDup(const char *s)
  165. #else
  166. char *StrDup(s)
  167. char *s;
  168. #endif
  169. {
  170.    char *ret = (char *) malloc(strlen(s)+1);
  171.    if (!ret) return (char *) NULL;
  172.    strcpy(ret, s);
  173.    return ret;
  174. }
  175.  
  176. /***************************************************************/
  177. /*                                                             */
  178. /*  StrCmpi                                                    */
  179. /*                                                             */
  180. /*  Compare strings, case insensitive.                         */
  181. /*                                                             */
  182. /***************************************************************/
  183. #ifdef HAVE_PROTOS
  184. PUBLIC int StrCmpi(char *s1, char *s2)
  185. #else
  186. int StrCmpi(s1, s2)
  187. char *s1, *s2;
  188. #endif
  189. {
  190.    int r;
  191.    while (*s1 && *s2) {
  192.       r = UPPER(*s1) - UPPER(*s2);
  193.       if (r) return r;
  194.       s1++;
  195.       s2++;
  196.    }
  197.    return UPPER(*s1) - UPPER(*s2);
  198. }
  199.  
  200. #ifdef NO_STRSTR
  201. #ifdef HAVE_PROTOS
  202. PUBLIC char *strstr(char *s1, char *s2)
  203. #else
  204. char *strstr(s1, s2)
  205. char *s1, *s2;
  206. #endif
  207. {
  208.    char *s = s1;
  209.    int len2 = strlen(s2);
  210.    int len1 = strlen(s1);
  211.  
  212.    while (s-s1 <= len1-len2) {
  213.       if (!strncmp(s, s2, len2)) return s;
  214.       s++;
  215.    }
  216.    return NULL;
  217. }
  218. #endif
  219.  
  220.